home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkListbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  68.2 KB  |  2,292 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkListbox.c --
  3.  *
  4.  *    This module implements listbox widgets for the Tk
  5.  *    toolkit.  A listbox displays a collection of strings,
  6.  *    one per line, and provides scrolling and selection.
  7.  *
  8.  * Copyright (c) 1990-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tkListbox.c 1.116 97/07/31 09:09:28
  15.  */
  16.  
  17. #include "tkPort.h"
  18. #include "default.h"
  19. #include "tkInt.h"
  20.  
  21. /*
  22.  * One record of the following type is kept for each element
  23.  * associated with a listbox widget:
  24.  */
  25.  
  26. typedef struct Element {
  27.     int textLength;        /* # non-NULL characters in text. */
  28.     int lBearing;        /* Distance from first character's
  29.                  * origin to left edge of character. */
  30.     int pixelWidth;        /* Total width of element in pixels (including
  31.                  * left bearing and right bearing). */
  32.     int selected;        /* 1 means this item is selected, 0 means
  33.                  * it isn't. */
  34.     struct Element *nextPtr;    /* Next in list of all elements of this
  35.                  * listbox, or NULL for last element. */
  36.     char text[4];        /* Characters of this element, NULL-
  37.                  * terminated.  The actual space allocated
  38.                  * here will be as large as needed (> 4,
  39.                  * most likely).  Must be the last field
  40.                  * of the record. */
  41. } Element;
  42.  
  43. #define ElementSize(stringLength) \
  44.     ((unsigned) (sizeof(Element) - 3 + stringLength))
  45.  
  46. /*
  47.  * A data structure of the following type is kept for each listbox
  48.  * widget managed by this file:
  49.  */
  50.  
  51. typedef struct {
  52.     Tk_Window tkwin;        /* Window that embodies the listbox.  NULL
  53.                  * means that the window has been destroyed
  54.                  * but the data structures haven't yet been
  55.                  * cleaned up.*/
  56.     Display *display;        /* Display containing widget.  Used, among
  57.                  * other things, so that resources can be
  58.                  * freed even after tkwin has gone away. */
  59.     Tcl_Interp *interp;        /* Interpreter associated with listbox. */
  60.     Tcl_Command widgetCmd;    /* Token for listbox's widget command. */
  61.     int numElements;        /* Total number of elements in this listbox. */
  62.     Element *firstPtr;        /* First in list of elements (NULL if no
  63.                  * elements). */
  64.     Element *lastPtr;        /* Last in list of elements (NULL if no
  65.                  * elements). */
  66.  
  67.     /*
  68.      * Information used when displaying widget:
  69.      */
  70.  
  71.     Tk_3DBorder normalBorder;    /* Used for drawing border around whole
  72.                  * window, plus used for background. */
  73.     int borderWidth;        /* Width of 3-D border around window. */
  74.     int relief;            /* 3-D effect: TK_RELIEF_RAISED, etc. */
  75.     int highlightWidth;        /* Width in pixels of highlight to draw
  76.                  * around widget when it has the focus.
  77.                  * <= 0 means don't draw a highlight. */
  78.     XColor *highlightBgColorPtr;
  79.                 /* Color for drawing traversal highlight
  80.                  * area when highlight is off. */
  81.     XColor *highlightColorPtr;    /* Color for drawing traversal highlight. */
  82.     int inset;            /* Total width of all borders, including
  83.                  * traversal highlight and 3-D border.
  84.                  * Indicates how much interior stuff must
  85.                  * be offset from outside edges to leave
  86.                  * room for borders. */
  87.     Tk_Font tkfont;        /* Information about text font, or NULL. */
  88.     XColor *fgColorPtr;        /* Text color in normal mode. */
  89.     GC textGC;            /* For drawing normal text. */
  90.     Tk_3DBorder selBorder;    /* Borders and backgrounds for selected
  91.                  * elements. */
  92.     int selBorderWidth;        /* Width of border around selection. */
  93.     XColor *selFgColorPtr;    /* Foreground color for selected elements. */
  94.     GC selTextGC;        /* For drawing selected text. */
  95.     int width;            /* Desired width of window, in characters. */
  96.     int height;            /* Desired height of window, in lines. */
  97.     int lineHeight;        /* Number of pixels allocated for each line
  98.                  * in display. */
  99.     int topIndex;        /* Index of top-most element visible in
  100.                  * window. */
  101.     int fullLines;        /* Number of lines that fit are completely
  102.                  * visible in window.  There may be one
  103.                  * additional line at the bottom that is
  104.                  * partially visible. */
  105.     int partialLine;        /* 0 means that the window holds exactly
  106.                  * fullLines lines.  1 means that there is
  107.                  * one additional line that is partially
  108.                  * visble. */
  109.     int setGrid;        /* Non-zero means pass gridding information
  110.                  * to window manager. */
  111.  
  112.     /*
  113.      * Information to support horizontal scrolling:
  114.      */
  115.  
  116.     int maxWidth;        /* Width (in pixels) of widest string in
  117.                  * listbox. */
  118.     int xScrollUnit;        /* Number of pixels in one "unit" for
  119.                  * horizontal scrolling (window scrolls
  120.                  * horizontally in increments of this size).
  121.                  * This is an average character size. */
  122.     int xOffset;        /* The left edge of each string in the
  123.                  * listbox is offset to the left by this
  124.                  * many pixels (0 means no offset, positive
  125.                  * means there is an offset). */
  126.  
  127.     /*
  128.      * Information about what's selected or active, if any.
  129.      */
  130.  
  131.     Tk_Uid selectMode;        /* Selection style: single, browse, multiple,
  132.                  * or extended.  This value isn't used in C
  133.                  * code, but the Tcl bindings use it. */
  134.     int numSelected;        /* Number of elements currently selected. */
  135.     int selectAnchor;        /* Fixed end of selection (i.e. element
  136.                  * at which selection was started.) */
  137.     int exportSelection;    /* Non-zero means tie internal listbox
  138.                  * to X selection. */
  139.     int active;            /* Index of "active" element (the one that
  140.                  * has been selected by keyboard traversal).
  141.                  * -1 means none. */
  142.  
  143.     /*
  144.      * Information for scanning:
  145.      */
  146.  
  147.     int scanMarkX;        /* X-position at which scan started (e.g.
  148.                  * button was pressed here). */
  149.     int scanMarkY;        /* Y-position at which scan started (e.g.
  150.                  * button was pressed here). */
  151.     int scanMarkXOffset;    /* Value of "xOffset" field when scan
  152.                  * started. */
  153.     int scanMarkYIndex;        /* Index of line that was at top of window
  154.                  * when scan started. */
  155.  
  156.     /*
  157.      * Miscellaneous information:
  158.      */
  159.  
  160.     Tk_Cursor cursor;        /* Current cursor for window, or None. */
  161.     char *takeFocus;        /* Value of -takefocus option;  not used in
  162.                  * the C code, but used by keyboard traversal
  163.                  * scripts.  Malloc'ed, but may be NULL. */
  164.     char *yScrollCmd;        /* Command prefix for communicating with
  165.                  * vertical scrollbar.  NULL means no command
  166.                  * to issue.  Malloc'ed. */
  167.     char *xScrollCmd;        /* Command prefix for communicating with
  168.                  * horizontal scrollbar.  NULL means no command
  169.                  * to issue.  Malloc'ed. */
  170.     int flags;            /* Various flag bits:  see below for
  171.                  * definitions. */
  172. } Listbox;
  173.  
  174. /*
  175.  * Flag bits for listboxes:
  176.  *
  177.  * REDRAW_PENDING:        Non-zero means a DoWhenIdle handler
  178.  *                has already been queued to redraw
  179.  *                this window.
  180.  * UPDATE_V_SCROLLBAR:        Non-zero means vertical scrollbar needs
  181.  *                to be updated.
  182.  * UPDATE_H_SCROLLBAR:        Non-zero means horizontal scrollbar needs
  183.  *                to be updated.
  184.  * GOT_FOCUS:            Non-zero means this widget currently
  185.  *                has the input focus.
  186.  */
  187.  
  188. #define REDRAW_PENDING        1
  189. #define UPDATE_V_SCROLLBAR    2
  190. #define UPDATE_H_SCROLLBAR    4
  191. #define GOT_FOCUS        8
  192.  
  193. /*
  194.  * Information used for argv parsing:
  195.  */
  196.  
  197. static Tk_ConfigSpec configSpecs[] = {
  198.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  199.     DEF_LISTBOX_BG_COLOR, Tk_Offset(Listbox, normalBorder),
  200.     TK_CONFIG_COLOR_ONLY},
  201.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  202.     DEF_LISTBOX_BG_MONO, Tk_Offset(Listbox, normalBorder),
  203.     TK_CONFIG_MONO_ONLY},
  204.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  205.     (char *) NULL, 0, 0},
  206.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  207.     (char *) NULL, 0, 0},
  208.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  209.     DEF_LISTBOX_BORDER_WIDTH, Tk_Offset(Listbox, borderWidth), 0},
  210.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  211.     DEF_LISTBOX_CURSOR, Tk_Offset(Listbox, cursor), TK_CONFIG_NULL_OK},
  212.     {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection",
  213.     "ExportSelection", DEF_LISTBOX_EXPORT_SELECTION,
  214.     Tk_Offset(Listbox, exportSelection), 0},
  215.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  216.     (char *) NULL, 0, 0},
  217.     {TK_CONFIG_FONT, "-font", "font", "Font",
  218.     DEF_LISTBOX_FONT, Tk_Offset(Listbox, tkfont), 0},
  219.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  220.     DEF_LISTBOX_FG, Tk_Offset(Listbox, fgColorPtr), 0},
  221.     {TK_CONFIG_INT, "-height", "height", "Height",
  222.     DEF_LISTBOX_HEIGHT, Tk_Offset(Listbox, height), 0},
  223.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  224.     "HighlightBackground", DEF_LISTBOX_HIGHLIGHT_BG,
  225.     Tk_Offset(Listbox, highlightBgColorPtr), 0},
  226.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  227.     DEF_LISTBOX_HIGHLIGHT, Tk_Offset(Listbox, highlightColorPtr), 0},
  228.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  229.     "HighlightThickness",
  230.     DEF_LISTBOX_HIGHLIGHT_WIDTH, Tk_Offset(Listbox, highlightWidth), 0},
  231.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  232.     DEF_LISTBOX_RELIEF, Tk_Offset(Listbox, relief), 0},
  233.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  234.     DEF_LISTBOX_SELECT_COLOR, Tk_Offset(Listbox, selBorder),
  235.     TK_CONFIG_COLOR_ONLY},
  236.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  237.     DEF_LISTBOX_SELECT_MONO, Tk_Offset(Listbox, selBorder),
  238.     TK_CONFIG_MONO_ONLY},
  239.     {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
  240.     DEF_LISTBOX_SELECT_BD, Tk_Offset(Listbox, selBorderWidth), 0},
  241.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  242.     DEF_LISTBOX_SELECT_FG_COLOR, Tk_Offset(Listbox, selFgColorPtr),
  243.     TK_CONFIG_COLOR_ONLY},
  244.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  245.     DEF_LISTBOX_SELECT_FG_MONO, Tk_Offset(Listbox, selFgColorPtr),
  246.     TK_CONFIG_MONO_ONLY},
  247.     {TK_CONFIG_UID, "-selectmode", "selectMode", "SelectMode",
  248.     DEF_LISTBOX_SELECT_MODE, Tk_Offset(Listbox, selectMode), 0},
  249.     {TK_CONFIG_BOOLEAN, "-setgrid", "setGrid", "SetGrid",
  250.     DEF_LISTBOX_SET_GRID, Tk_Offset(Listbox, setGrid), 0},
  251.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  252.     DEF_LISTBOX_TAKE_FOCUS, Tk_Offset(Listbox, takeFocus),
  253.     TK_CONFIG_NULL_OK},
  254.     {TK_CONFIG_INT, "-width", "width", "Width",
  255.     DEF_LISTBOX_WIDTH, Tk_Offset(Listbox, width), 0},
  256.     {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  257.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, xScrollCmd),
  258.     TK_CONFIG_NULL_OK},
  259.     {TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
  260.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, yScrollCmd),
  261.     TK_CONFIG_NULL_OK},
  262.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  263.     (char *) NULL, 0, 0}
  264. };
  265.  
  266. /*
  267.  * Forward declarations for procedures defined later in this file:
  268.  */
  269.  
  270. static void        ChangeListboxOffset _ANSI_ARGS_((Listbox *listPtr,
  271.                 int offset));
  272. static void        ChangeListboxView _ANSI_ARGS_((Listbox *listPtr,
  273.                 int index));
  274. static int        ConfigureListbox _ANSI_ARGS_((Tcl_Interp *interp,
  275.                 Listbox *listPtr, int argc, char **argv,
  276.                 int flags));
  277. static void        DeleteEls _ANSI_ARGS_((Listbox *listPtr, int first,
  278.                 int last));
  279. static void        DestroyListbox _ANSI_ARGS_((char *memPtr));
  280. static void        DisplayListbox _ANSI_ARGS_((ClientData clientData));
  281. static int        GetListboxIndex _ANSI_ARGS_((Tcl_Interp *interp,
  282.                 Listbox *listPtr, char *string, int numElsOK,
  283.                 int *indexPtr));
  284. static void        InsertEls _ANSI_ARGS_((Listbox *listPtr, int index,
  285.                 int argc, char **argv));
  286. static void        ListboxCmdDeletedProc _ANSI_ARGS_((
  287.                 ClientData clientData));
  288. static void        ListboxComputeGeometry _ANSI_ARGS_((Listbox *listPtr,
  289.                 int fontChanged, int maxIsStale, int updateGrid));
  290. static void        ListboxEventProc _ANSI_ARGS_((ClientData clientData,
  291.                 XEvent *eventPtr));
  292. static int        ListboxFetchSelection _ANSI_ARGS_((
  293.                 ClientData clientData, int offset, char *buffer,
  294.                 int maxBytes));
  295. static void        ListboxLostSelection _ANSI_ARGS_((
  296.                 ClientData clientData));
  297. static void        ListboxRedrawRange _ANSI_ARGS_((Listbox *listPtr,
  298.                 int first, int last));
  299. static void        ListboxScanTo _ANSI_ARGS_((Listbox *listPtr,
  300.                 int x, int y));
  301. static void        ListboxSelect _ANSI_ARGS_((Listbox *listPtr,
  302.                 int first, int last, int select));
  303. static void        ListboxUpdateHScrollbar _ANSI_ARGS_((Listbox *listPtr));
  304. static void        ListboxUpdateVScrollbar _ANSI_ARGS_((Listbox *listPtr));
  305. static int        ListboxWidgetCmd _ANSI_ARGS_((ClientData clientData,
  306.                 Tcl_Interp *interp, int argc, char **argv));
  307. static void        ListboxWorldChanged _ANSI_ARGS_((
  308.                 ClientData instanceData));
  309. static int        NearestListboxElement _ANSI_ARGS_((Listbox *listPtr,
  310.                 int y));
  311.  
  312. /*
  313.  * The structure below defines button class behavior by means of procedures
  314.  * that can be invoked from generic window code.
  315.  */
  316.  
  317. static TkClassProcs listboxClass = {
  318.     NULL,            /* createProc. */
  319.     ListboxWorldChanged,    /* geometryProc. */
  320.     NULL            /* modalProc. */
  321. };
  322.  
  323.  
  324. /*
  325.  *--------------------------------------------------------------
  326.  *
  327.  * Tk_ListboxCmd --
  328.  *
  329.  *    This procedure is invoked to process the "listbox" Tcl
  330.  *    command.  See the user documentation for details on what
  331.  *    it does.
  332.  *
  333.  * Results:
  334.  *    A standard Tcl result.
  335.  *
  336.  * Side effects:
  337.  *    See the user documentation.
  338.  *
  339.  *--------------------------------------------------------------
  340.  */
  341.  
  342. int
  343. Tk_ListboxCmd(clientData, interp, argc, argv)
  344.     ClientData clientData;    /* Main window associated with
  345.                  * interpreter. */
  346.     Tcl_Interp *interp;        /* Current interpreter. */
  347.     int argc;            /* Number of arguments. */
  348.     char **argv;        /* Argument strings. */
  349. {
  350.     register Listbox *listPtr;
  351.     Tk_Window new;
  352.     Tk_Window tkwin = (Tk_Window) clientData;
  353.  
  354.     if (argc < 2) {
  355.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  356.         argv[0], " pathName ?options?\"", (char *) NULL);
  357.     return TCL_ERROR;
  358.     }
  359.  
  360.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  361.     if (new == NULL) {
  362.     return TCL_ERROR;
  363.     }
  364.  
  365.     /*
  366.      * Initialize the fields of the structure that won't be initialized
  367.      * by ConfigureListbox, or that ConfigureListbox requires to be
  368.      * initialized already (e.g. resource pointers).
  369.      */
  370.  
  371.     listPtr = (Listbox *) ckalloc(sizeof(Listbox));
  372.     listPtr->tkwin = new;
  373.     listPtr->display = Tk_Display(new);
  374.     listPtr->interp = interp;
  375.     listPtr->widgetCmd = Tcl_CreateCommand(interp,
  376.         Tk_PathName(listPtr->tkwin), ListboxWidgetCmd,
  377.         (ClientData) listPtr, ListboxCmdDeletedProc);
  378.     listPtr->numElements = 0;
  379.     listPtr->firstPtr = NULL;
  380.     listPtr->lastPtr = NULL;
  381.     listPtr->normalBorder = NULL;
  382.     listPtr->borderWidth = 0;
  383.     listPtr->relief = TK_RELIEF_RAISED;
  384.     listPtr->highlightWidth = 0;
  385.     listPtr->highlightBgColorPtr = NULL;
  386.     listPtr->highlightColorPtr = NULL;
  387.     listPtr->inset = 0;
  388.     listPtr->tkfont = NULL;
  389.     listPtr->fgColorPtr = NULL;
  390.     listPtr->textGC = None;
  391.     listPtr->selBorder = NULL;
  392.     listPtr->selBorderWidth = 0;
  393.     listPtr->selFgColorPtr = None;
  394.     listPtr->selTextGC = None;
  395.     listPtr->width = 0;
  396.     listPtr->height = 0;
  397.     listPtr->lineHeight = 0;
  398.     listPtr->topIndex = 0;
  399.     listPtr->fullLines = 1;
  400.     listPtr->partialLine = 0;
  401.     listPtr->setGrid = 0;
  402.     listPtr->maxWidth = 0;
  403.     listPtr->xScrollUnit = 0;
  404.     listPtr->xOffset = 0;
  405.     listPtr->selectMode = NULL;
  406.     listPtr->numSelected = 0;
  407.     listPtr->selectAnchor = 0;
  408.     listPtr->exportSelection = 1;
  409.     listPtr->active = 0;
  410.     listPtr->scanMarkX = 0;
  411.     listPtr->scanMarkY = 0;
  412.     listPtr->scanMarkXOffset = 0;
  413.     listPtr->scanMarkYIndex = 0;
  414.     listPtr->cursor = None;
  415.     listPtr->takeFocus = NULL;
  416.     listPtr->xScrollCmd = NULL;
  417.     listPtr->yScrollCmd = NULL;
  418.     listPtr->flags = 0;
  419.  
  420.     Tk_SetClass(listPtr->tkwin, "Listbox");
  421.     TkSetClassProcs(listPtr->tkwin, &listboxClass, (ClientData) listPtr);
  422.     Tk_CreateEventHandler(listPtr->tkwin,
  423.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  424.         ListboxEventProc, (ClientData) listPtr);
  425.     Tk_CreateSelHandler(listPtr->tkwin, XA_PRIMARY, XA_STRING,
  426.         ListboxFetchSelection, (ClientData) listPtr, XA_STRING);
  427.     if (ConfigureListbox(interp, listPtr, argc-2, argv+2, 0) != TCL_OK) {
  428.     goto error;
  429.     }
  430.  
  431.     interp->result = Tk_PathName(listPtr->tkwin);
  432.     return TCL_OK;
  433.  
  434.     error:
  435.     Tk_DestroyWindow(listPtr->tkwin);
  436.     return TCL_ERROR;
  437. }
  438.  
  439. /*
  440.  *--------------------------------------------------------------
  441.  *
  442.  * ListboxWidgetCmd --
  443.  *
  444.  *    This procedure is invoked to process the Tcl command
  445.  *    that corresponds to a widget managed by this module.
  446.  *    See the user documentation for details on what it does.
  447.  *
  448.  * Results:
  449.  *    A standard Tcl result.
  450.  *
  451.  * Side effects:
  452.  *    See the user documentation.
  453.  *
  454.  *--------------------------------------------------------------
  455.  */
  456.  
  457. static int
  458. ListboxWidgetCmd(clientData, interp, argc, argv)
  459.     ClientData clientData;        /* Information about listbox widget. */
  460.     Tcl_Interp *interp;            /* Current interpreter. */
  461.     int argc;                /* Number of arguments. */
  462.     char **argv;            /* Argument strings. */
  463. {
  464.     register Listbox *listPtr = (Listbox *) clientData;
  465.     int result = TCL_OK;
  466.     size_t length;
  467.     int c;
  468.     Tk_FontMetrics fm;
  469.  
  470.     if (argc < 2) {
  471.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  472.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  473.     return TCL_ERROR;
  474.     }
  475.     Tcl_Preserve((ClientData) listPtr);
  476.     c = argv[1][0];
  477.     length = strlen(argv[1]);
  478.     if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)) {
  479.     int index;
  480.  
  481.     if (argc != 3) {
  482.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  483.             argv[0], " activate index\"",
  484.             (char *) NULL);
  485.         goto error;
  486.     }
  487.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  488.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  489.         != TCL_OK) {
  490.         goto error;
  491.     }
  492.     listPtr->active = index;
  493.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  494.     } else if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
  495.     int index, x, y, i;
  496.     Element *elPtr;
  497.  
  498.     if (argc != 3) {
  499.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  500.             argv[0], " bbox index\"", (char *) NULL);
  501.         goto error;
  502.     }
  503.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  504.         goto error;
  505.     }
  506.     for (i = 0, elPtr = listPtr->firstPtr; i < index;
  507.         i++, elPtr = elPtr->nextPtr) {
  508.         /* Empty loop body. */
  509.     }
  510.     if ((index >= listPtr->topIndex) && (index < listPtr->numElements)
  511.             && (index < (listPtr->topIndex + listPtr->fullLines
  512.             + listPtr->partialLine))) {
  513.         x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
  514.         y = ((index - listPtr->topIndex)*listPtr->lineHeight)
  515.             + listPtr->inset + listPtr->selBorderWidth;
  516.         Tk_GetFontMetrics(listPtr->tkfont, &fm);
  517.         sprintf(interp->result, "%d %d %d %d", x, y, elPtr->pixelWidth,
  518.             fm.linespace);
  519.     }
  520.     } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  521.         && (length >= 2)) {
  522.     if (argc != 3) {
  523.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  524.             argv[0], " cget option\"",
  525.             (char *) NULL);
  526.         goto error;
  527.     }
  528.     result = Tk_ConfigureValue(interp, listPtr->tkwin, configSpecs,
  529.         (char *) listPtr, argv[2], 0);
  530.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  531.         && (length >= 2)) {
  532.     if (argc == 2) {
  533.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  534.             (char *) listPtr, (char *) NULL, 0);
  535.     } else if (argc == 3) {
  536.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  537.             (char *) listPtr, argv[2], 0);
  538.     } else {
  539.         result = ConfigureListbox(interp, listPtr, argc-2, argv+2,
  540.             TK_CONFIG_ARGV_ONLY);
  541.     }
  542.     } else if ((c == 'c') && (strncmp(argv[1], "curselection", length) == 0)
  543.         && (length >= 2)) {
  544.     int i, count;
  545.     char index[20];
  546.     Element *elPtr;
  547.  
  548.     if (argc != 2) {
  549.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  550.             argv[0], " curselection\"",
  551.             (char *) NULL);
  552.         goto error;
  553.     }
  554.     count = 0;
  555.     for (i = 0, elPtr = listPtr->firstPtr; elPtr != NULL;
  556.         i++, elPtr = elPtr->nextPtr) {
  557.         if (elPtr->selected) {
  558.         sprintf(index, "%d", i);
  559.         Tcl_AppendElement(interp, index);
  560.         count++;
  561.         }
  562.     }
  563.     if (count != listPtr->numSelected) {
  564.         panic("ListboxWidgetCmd: selection count incorrect");
  565.     }
  566.     } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
  567.     int first, last;
  568.  
  569.     if ((argc < 3) || (argc > 4)) {
  570.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  571.             argv[0], " delete firstIndex ?lastIndex?\"",
  572.             (char *) NULL);
  573.         goto error;
  574.     }
  575.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  576.         goto error;
  577.     }
  578.     if (argc == 3) {
  579.         last = first;
  580.     } else {
  581.         if (GetListboxIndex(interp, listPtr, argv[3], 0, &last) != TCL_OK) {
  582.         goto error;
  583.         }
  584.     }
  585.     DeleteEls(listPtr, first, last);
  586.     } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  587.     int first, last, i;
  588.     Element *elPtr;
  589.  
  590.     if ((argc != 3) && (argc != 4)) {
  591.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  592.             argv[0], " get first ?last?\"", (char *) NULL);
  593.         goto error;
  594.     }
  595.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  596.         goto error;
  597.     }
  598.     if ((argc == 4) && (GetListboxIndex(interp, listPtr, argv[3],
  599.         0, &last) != TCL_OK)) {
  600.         goto error;
  601.     }
  602.     for (elPtr = listPtr->firstPtr, i = 0; i < first;
  603.         i++, elPtr = elPtr->nextPtr) {
  604.         /* Empty loop body. */
  605.     }
  606.     if (elPtr != NULL) {
  607.         if (argc == 3) {
  608.         interp->result = elPtr->text;
  609.         } else {
  610.         for (  ; i <= last; i++, elPtr = elPtr->nextPtr) {
  611.             Tcl_AppendElement(interp, elPtr->text);
  612.         }
  613.         }
  614.     }
  615.     } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
  616.         && (length >= 3)) {
  617.     int index;
  618.  
  619.     if (argc != 3) {
  620.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  621.             argv[0], " index index\"",
  622.             (char *) NULL);
  623.         goto error;
  624.     }
  625.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  626.         != TCL_OK) {
  627.         goto error;
  628.     }
  629.     sprintf(interp->result, "%d", index);
  630.     } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
  631.         && (length >= 3)) {
  632.     int index;
  633.  
  634.     if (argc < 3) {
  635.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  636.             argv[0], " insert index ?element element ...?\"",
  637.             (char *) NULL);
  638.         goto error;
  639.     }
  640.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  641.         != TCL_OK) {
  642.         goto error;
  643.     }
  644.     InsertEls(listPtr, index, argc-3, argv+3);
  645.     } else if ((c == 'n') && (strncmp(argv[1], "nearest", length) == 0)) {
  646.     int index, y;
  647.  
  648.     if (argc != 3) {
  649.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  650.             argv[0], " nearest y\"", (char *) NULL);
  651.         goto error;
  652.     }
  653.     if (Tcl_GetInt(interp, argv[2], &y) != TCL_OK) {
  654.         goto error;
  655.     }
  656.     index = NearestListboxElement(listPtr, y);
  657.     sprintf(interp->result, "%d", index);
  658.     } else if ((c == 's') && (length >= 2)
  659.         && (strncmp(argv[1], "scan", length) == 0)) {
  660.     int x, y;
  661.  
  662.     if (argc != 5) {
  663.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  664.             argv[0], " scan mark|dragto x y\"", (char *) NULL);
  665.         goto error;
  666.     }
  667.     if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK)
  668.         || (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
  669.         goto error;
  670.     }
  671.     if ((argv[2][0] == 'm')
  672.         && (strncmp(argv[2], "mark", strlen(argv[2])) == 0)) {
  673.         listPtr->scanMarkX = x;
  674.         listPtr->scanMarkY = y;
  675.         listPtr->scanMarkXOffset = listPtr->xOffset;
  676.         listPtr->scanMarkYIndex = listPtr->topIndex;
  677.     } else if ((argv[2][0] == 'd')
  678.         && (strncmp(argv[2], "dragto", strlen(argv[2])) == 0)) {
  679.         ListboxScanTo(listPtr, x, y);
  680.     } else {
  681.         Tcl_AppendResult(interp, "bad scan option \"", argv[2],
  682.             "\": must be mark or dragto", (char *) NULL);
  683.         goto error;
  684.     }
  685.     } else if ((c == 's') && (strncmp(argv[1], "see", length) == 0)
  686.         && (length >= 3)) {
  687.     int index, diff;
  688.     if (argc != 3) {
  689.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  690.             argv[0], " see index\"",
  691.             (char *) NULL);
  692.         goto error;
  693.     }
  694.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  695.         goto error;
  696.     }
  697.     diff = listPtr->topIndex-index;
  698.     if (diff > 0) {
  699.         if (diff <= (listPtr->fullLines/3)) {
  700.         ChangeListboxView(listPtr, index);
  701.         } else {
  702.         ChangeListboxView(listPtr, index - (listPtr->fullLines-1)/2);
  703.         }
  704.     } else {
  705.         diff = index - (listPtr->topIndex + listPtr->fullLines - 1);
  706.         if (diff > 0) {
  707.         if (diff <= (listPtr->fullLines/3)) {
  708.             ChangeListboxView(listPtr, listPtr->topIndex + diff);
  709.         } else {
  710.             ChangeListboxView(listPtr,
  711.                 index - (listPtr->fullLines-1)/2);
  712.         }
  713.         }
  714.     }
  715.     } else if ((c == 's') && (length >= 3)
  716.         && (strncmp(argv[1], "selection", length) == 0)) {
  717.     int first, last;
  718.  
  719.     if ((argc != 4) && (argc != 5)) {
  720.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  721.             argv[0], " selection option index ?index?\"",
  722.             (char *) NULL);
  723.         goto error;
  724.     }
  725.     if (GetListboxIndex(interp, listPtr, argv[3], 0, &first) != TCL_OK) {
  726.         goto error;
  727.     }
  728.     if (argc == 5) {
  729.         if (GetListboxIndex(interp, listPtr, argv[4], 0, &last) != TCL_OK) {
  730.         goto error;
  731.         }
  732.     } else {
  733.         last = first;
  734.     }
  735.     length = strlen(argv[2]);
  736.     c = argv[2][0];
  737.     if ((c == 'a') && (strncmp(argv[2], "anchor", length) == 0)) {
  738.         if (argc != 4) {
  739.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  740.             argv[0], " selection anchor index\"", (char *) NULL);
  741.         goto error;
  742.         }
  743.         listPtr->selectAnchor = first;
  744.     } else if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
  745.         ListboxSelect(listPtr, first, last, 0);
  746.     } else if ((c == 'i') && (strncmp(argv[2], "includes", length) == 0)) {
  747.         int i;
  748.         Element *elPtr;
  749.     
  750.         if (argc != 4) {
  751.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  752.             argv[0], " selection includes index\"", (char *) NULL);
  753.         goto error;
  754.         }
  755.         for (elPtr = listPtr->firstPtr, i = 0; i < first;
  756.             i++, elPtr = elPtr->nextPtr) {
  757.         /* Empty loop body. */
  758.         }
  759.         if ((elPtr != NULL) && (elPtr->selected)) {
  760.         interp->result = "1";
  761.         } else {
  762.         interp->result = "0";
  763.         }
  764.     } else if ((c == 's') && (strncmp(argv[2], "set", length) == 0)) {
  765.         ListboxSelect(listPtr, first, last, 1);
  766.     } else {
  767.         Tcl_AppendResult(interp, "bad selection option \"", argv[2],
  768.             "\": must be anchor, clear, includes, or set",
  769.             (char *) NULL);
  770.         goto error;
  771.     }
  772.     } else if ((c == 's') && (length >= 2)
  773.         && (strncmp(argv[1], "size", length) == 0)) {
  774.     if (argc != 2) {
  775.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  776.             argv[0], " size\"", (char *) NULL);
  777.         goto error;
  778.     }
  779.     sprintf(interp->result, "%d", listPtr->numElements);
  780.     } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
  781.     int index, count, type, windowWidth, windowUnits;
  782.     int offset = 0;        /* Initialized to stop gcc warnings. */
  783.     double fraction, fraction2;
  784.  
  785.     windowWidth = Tk_Width(listPtr->tkwin)
  786.         - 2*(listPtr->inset + listPtr->selBorderWidth);
  787.     if (argc == 2) {
  788.         if (listPtr->maxWidth == 0) {
  789.         interp->result = "0 1";
  790.         } else {
  791.         fraction = listPtr->xOffset/((double) listPtr->maxWidth);
  792.         fraction2 = (listPtr->xOffset + windowWidth)
  793.             /((double) listPtr->maxWidth);
  794.         if (fraction2 > 1.0) {
  795.             fraction2 = 1.0;
  796.         }
  797.         sprintf(interp->result, "%g %g", fraction, fraction2);
  798.         }
  799.     } else if (argc == 3) {
  800.         if (Tcl_GetInt(interp, argv[2], &index) != TCL_OK) {
  801.         goto error;
  802.         }
  803.         ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
  804.     } else {
  805.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  806.         switch (type) {
  807.         case TK_SCROLL_ERROR:
  808.             goto error;
  809.         case TK_SCROLL_MOVETO:
  810.             offset = (int) (fraction*listPtr->maxWidth + 0.5);
  811.             break;
  812.         case TK_SCROLL_PAGES:
  813.             windowUnits = windowWidth/listPtr->xScrollUnit;
  814.             if (windowUnits > 2) {
  815.             offset = listPtr->xOffset
  816.                 + count*listPtr->xScrollUnit*(windowUnits-2);
  817.             } else {
  818.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  819.             }
  820.             break;
  821.         case TK_SCROLL_UNITS:
  822.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  823.             break;
  824.         }
  825.         ChangeListboxOffset(listPtr, offset);
  826.     }
  827.     } else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)) {
  828.     int index, count, type;
  829.     double fraction, fraction2;
  830.  
  831.     if (argc == 2) {
  832.         if (listPtr->numElements == 0) {
  833.         interp->result = "0 1";
  834.         } else {
  835.         fraction = listPtr->topIndex/((double) listPtr->numElements);
  836.         fraction2 = (listPtr->topIndex+listPtr->fullLines)
  837.             /((double) listPtr->numElements);
  838.         if (fraction2 > 1.0) {
  839.             fraction2 = 1.0;
  840.         }
  841.         sprintf(interp->result, "%g %g", fraction, fraction2);
  842.         }
  843.     } else if (argc == 3) {
  844.         if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  845.             != TCL_OK) {
  846.         goto error;
  847.         }
  848.         ChangeListboxView(listPtr, index);
  849.     } else {
  850.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  851.         switch (type) {
  852.         case TK_SCROLL_ERROR:
  853.             goto error;
  854.         case TK_SCROLL_MOVETO:
  855.             index = (int) (listPtr->numElements*fraction + 0.5);
  856.             break;
  857.         case TK_SCROLL_PAGES:
  858.             if (listPtr->fullLines > 2) {
  859.             index = listPtr->topIndex
  860.                 + count*(listPtr->fullLines-2);
  861.             } else {
  862.             index = listPtr->topIndex + count;
  863.             }
  864.             break;
  865.         case TK_SCROLL_UNITS:
  866.             index = listPtr->topIndex + count;
  867.             break;
  868.         }
  869.         ChangeListboxView(listPtr, index);
  870.     }
  871.     } else {
  872.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  873.         "\": must be activate, bbox, cget, configure, ",
  874.         "curselection, delete, get, index, insert, nearest, ",
  875.         "scan, see, selection, size, ",
  876.         "xview, or yview", (char *) NULL);
  877.     goto error;
  878.     }
  879.     Tcl_Release((ClientData) listPtr);
  880.     return result;
  881.  
  882.     error:
  883.     Tcl_Release((ClientData) listPtr);
  884.     return TCL_ERROR;
  885. }
  886.  
  887. /*
  888.  *----------------------------------------------------------------------
  889.  *
  890.  * DestroyListbox --
  891.  *
  892.  *    This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
  893.  *    to clean up the internal structure of a listbox at a safe time
  894.  *    (when no-one is using it anymore).
  895.  *
  896.  * Results:
  897.  *    None.
  898.  *
  899.  * Side effects:
  900.  *    Everything associated with the listbox is freed up.
  901.  *
  902.  *----------------------------------------------------------------------
  903.  */
  904.  
  905. static void
  906. DestroyListbox(memPtr)
  907.     char *memPtr;    /* Info about listbox widget. */
  908. {
  909.     register Listbox *listPtr = (Listbox *) memPtr;
  910.     register Element *elPtr, *nextPtr;
  911.  
  912.     /*
  913.      * Free up all of the list elements.
  914.      */
  915.  
  916.     for (elPtr = listPtr->firstPtr; elPtr != NULL; ) {
  917.     nextPtr = elPtr->nextPtr;
  918.     ckfree((char *) elPtr);
  919.     elPtr = nextPtr;
  920.     }
  921.  
  922.     /*
  923.      * Free up all the stuff that requires special handling, then
  924.      * let Tk_FreeOptions handle all the standard option-related
  925.      * stuff.
  926.      */
  927.  
  928.     if (listPtr->textGC != None) {
  929.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  930.     }
  931.     if (listPtr->selTextGC != None) {
  932.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  933.     }
  934.     Tk_FreeOptions(configSpecs, (char *) listPtr, listPtr->display, 0);
  935.     ckfree((char *) listPtr);
  936. }
  937.  
  938. /*
  939.  *----------------------------------------------------------------------
  940.  *
  941.  * ConfigureListbox --
  942.  *
  943.  *    This procedure is called to process an argv/argc list, plus
  944.  *    the Tk option database, in order to configure (or reconfigure)
  945.  *    a listbox widget.
  946.  *
  947.  * Results:
  948.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  949.  *    returned, then interp->result contains an error message.
  950.  *
  951.  * Side effects:
  952.  *    Configuration information, such as colors, border width,
  953.  *    etc. get set for listPtr;  old resources get freed,
  954.  *    if there were any.
  955.  *
  956.  *----------------------------------------------------------------------
  957.  */
  958.  
  959. static int
  960. ConfigureListbox(interp, listPtr, argc, argv, flags)
  961.     Tcl_Interp *interp;        /* Used for error reporting. */
  962.     register Listbox *listPtr;    /* Information about widget;  may or may
  963.                  * not already have values for some fields. */
  964.     int argc;            /* Number of valid entries in argv. */
  965.     char **argv;        /* Arguments. */
  966.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  967. {
  968.     int oldExport;
  969.  
  970.     oldExport = listPtr->exportSelection;
  971.     if (Tk_ConfigureWidget(interp, listPtr->tkwin, configSpecs,
  972.         argc, argv, (char *) listPtr, flags) != TCL_OK) {
  973.     return TCL_ERROR;
  974.     }
  975.  
  976.     /*
  977.      * A few options need special processing, such as setting the
  978.      * background from a 3-D border.
  979.      */
  980.  
  981.     Tk_SetBackgroundFromBorder(listPtr->tkwin, listPtr->normalBorder);
  982.  
  983.     if (listPtr->highlightWidth < 0) {
  984.     listPtr->highlightWidth = 0;
  985.     }
  986.     listPtr->inset = listPtr->highlightWidth + listPtr->borderWidth;
  987.  
  988.     /*
  989.      * Claim the selection if we've suddenly started exporting it and
  990.      * there is a selection to export.
  991.      */
  992.  
  993.     if (listPtr->exportSelection && !oldExport
  994.         && (listPtr->numSelected != 0)) {
  995.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  996.         (ClientData) listPtr);
  997.     }
  998.  
  999.     ListboxWorldChanged((ClientData) listPtr);
  1000.     return TCL_OK;
  1001. }
  1002.  
  1003. /*
  1004.  *---------------------------------------------------------------------------
  1005.  *
  1006.  * ListboxWorldChanged --
  1007.  *
  1008.  *      This procedure is called when the world has changed in some
  1009.  *      way and the widget needs to recompute all its graphics contexts
  1010.  *    and determine its new geometry.
  1011.  *
  1012.  * Results:
  1013.  *      None.
  1014.  *
  1015.  * Side effects:
  1016.  *      Listbox will be relayed out and redisplayed.
  1017.  *
  1018.  *---------------------------------------------------------------------------
  1019.  */
  1020.  
  1021. static void
  1022. ListboxWorldChanged(instanceData)
  1023.     ClientData instanceData;    /* Information about widget. */
  1024. {
  1025.     XGCValues gcValues;
  1026.     GC gc;
  1027.     unsigned long mask;
  1028.     Listbox *listPtr;
  1029.  
  1030.     listPtr = (Listbox *) instanceData;
  1031.  
  1032.     gcValues.foreground = listPtr->fgColorPtr->pixel;
  1033.     gcValues.font = Tk_FontId(listPtr->tkfont);
  1034.     gcValues.graphics_exposures = False;
  1035.     mask = GCForeground | GCFont | GCGraphicsExposures;
  1036.     gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
  1037.     if (listPtr->textGC != None) {
  1038.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  1039.     }
  1040.     listPtr->textGC = gc;
  1041.  
  1042.     gcValues.foreground = listPtr->selFgColorPtr->pixel;
  1043.     gcValues.font = Tk_FontId(listPtr->tkfont);
  1044.     mask = GCForeground | GCFont;
  1045.     gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
  1046.     if (listPtr->selTextGC != None) {
  1047.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  1048.     }
  1049.     listPtr->selTextGC = gc;
  1050.  
  1051.     /*
  1052.      * Register the desired geometry for the window and arrange for
  1053.      * the window to be redisplayed.
  1054.      */
  1055.  
  1056.     ListboxComputeGeometry(listPtr, 1, 1, 1);
  1057.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1058.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1059. }
  1060.  
  1061. /*
  1062.  *--------------------------------------------------------------
  1063.  *
  1064.  * DisplayListbox --
  1065.  *
  1066.  *    This procedure redraws the contents of a listbox window.
  1067.  *
  1068.  * Results:
  1069.  *    None.
  1070.  *
  1071.  * Side effects:
  1072.  *    Information appears on the screen.
  1073.  *
  1074.  *--------------------------------------------------------------
  1075.  */
  1076.  
  1077. static void
  1078. DisplayListbox(clientData)
  1079.     ClientData clientData;    /* Information about window. */
  1080. {
  1081.     register Listbox *listPtr = (Listbox *) clientData;
  1082.     register Tk_Window tkwin = listPtr->tkwin;
  1083.     register Element *elPtr;
  1084.     GC gc;
  1085.     int i, limit, x, y, width, prevSelected;
  1086.     Tk_FontMetrics fm;
  1087.     int left, right;            /* Non-zero values here indicate
  1088.                      * that the left or right edge of
  1089.                      * the listbox is off-screen. */
  1090.     Pixmap pixmap;
  1091.  
  1092.     listPtr->flags &= ~REDRAW_PENDING;
  1093.     if (listPtr->flags & UPDATE_V_SCROLLBAR) {
  1094.     ListboxUpdateVScrollbar(listPtr);
  1095.     }
  1096.     if (listPtr->flags & UPDATE_H_SCROLLBAR) {
  1097.     ListboxUpdateHScrollbar(listPtr);
  1098.     }
  1099.     listPtr->flags &= ~(REDRAW_PENDING|UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR);
  1100.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
  1101.     return;
  1102.     }
  1103.  
  1104.     /*
  1105.      * Redrawing is done in a temporary pixmap that is allocated
  1106.      * here and freed at the end of the procedure.  All drawing is
  1107.      * done to the pixmap, and the pixmap is copied to the screen
  1108.      * at the end of the procedure.  This provides the smoothest
  1109.      * possible visual effects (no flashing on the screen).
  1110.      */
  1111.  
  1112.     pixmap = Tk_GetPixmap(listPtr->display, Tk_WindowId(tkwin),
  1113.         Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
  1114.     Tk_Fill3DRectangle(tkwin, pixmap, listPtr->normalBorder, 0, 0,
  1115.         Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
  1116.  
  1117.     /*
  1118.      * Iterate through all of the elements of the listbox, displaying each
  1119.      * in turn.  Selected elements use a different GC and have a raised
  1120.      * background.
  1121.      */
  1122.  
  1123.     limit = listPtr->topIndex + listPtr->fullLines + listPtr->partialLine - 1;
  1124.     if (limit >= listPtr->numElements) {
  1125.     limit = listPtr->numElements-1;
  1126.     }
  1127.     left = right = 0;
  1128.     if (listPtr->xOffset > 0) {
  1129.     left = listPtr->selBorderWidth+1;
  1130.     }
  1131.     if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
  1132.         - 2*(listPtr->inset + listPtr->selBorderWidth)))  {
  1133.     right = listPtr->selBorderWidth+1;
  1134.     }
  1135.     prevSelected = 0;
  1136.     for (elPtr = listPtr->firstPtr, i = 0; (elPtr != NULL) && (i <= limit);
  1137.         prevSelected = elPtr->selected, elPtr = elPtr->nextPtr, i++) {
  1138.     if (i < listPtr->topIndex) {
  1139.         continue;
  1140.     }
  1141.     x = listPtr->inset;
  1142.     y = ((i - listPtr->topIndex) * listPtr->lineHeight) 
  1143.         + listPtr->inset;
  1144.     gc = listPtr->textGC;
  1145.     if (elPtr->selected) {
  1146.         gc = listPtr->selTextGC;
  1147.         width = Tk_Width(tkwin) - 2*listPtr->inset;
  1148.         Tk_Fill3DRectangle(tkwin, pixmap, listPtr->selBorder, x, y,
  1149.             width, listPtr->lineHeight, 0, TK_RELIEF_FLAT);
  1150.  
  1151.         /*
  1152.          * Draw beveled edges around the selection, if there are visible
  1153.          * edges next to this element.  Special considerations:
  1154.          * 1. The left and right bevels may not be visible if horizontal
  1155.          *    scrolling is enabled (the "left" and "right" variables
  1156.          *    are zero to indicate that the corresponding bevel is
  1157.          *    visible).
  1158.          * 2. Top and bottom bevels are only drawn if this is the
  1159.          *    first or last seleted item.
  1160.          * 3. If the left or right bevel isn't visible, then the "left"
  1161.          *    and "right" variables, computed above, have non-zero values
  1162.          *    that extend the top and bottom bevels so that the mitered
  1163.          *    corners are off-screen.
  1164.          */
  1165.  
  1166.         if (left == 0) {
  1167.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1168.             x, y, listPtr->selBorderWidth, listPtr->lineHeight,
  1169.             1, TK_RELIEF_RAISED);
  1170.         }
  1171.         if (right == 0) {
  1172.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1173.             x + width - listPtr->selBorderWidth, y,
  1174.             listPtr->selBorderWidth, listPtr->lineHeight,
  1175.             0, TK_RELIEF_RAISED);
  1176.         }
  1177.         if (!prevSelected) {
  1178.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder,
  1179.             x-left, y, width+left+right, listPtr->selBorderWidth,
  1180.             1, 1, 1, TK_RELIEF_RAISED);
  1181.         }
  1182.         if ((elPtr->nextPtr == NULL) || !elPtr->nextPtr->selected) {
  1183.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder, x-left,
  1184.             y + listPtr->lineHeight - listPtr->selBorderWidth,
  1185.             width+left+right, listPtr->selBorderWidth, 0, 0, 0,
  1186.             TK_RELIEF_RAISED);
  1187.         }
  1188.     }
  1189.     Tk_GetFontMetrics(listPtr->tkfont, &fm);
  1190.     y += fm.ascent + listPtr->selBorderWidth;
  1191.     x = listPtr->inset + listPtr->selBorderWidth - elPtr->lBearing
  1192.         - listPtr->xOffset;
  1193.     Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont,
  1194.         elPtr->text, elPtr->textLength, x, y);
  1195.  
  1196.     /*
  1197.      * If this is the active element, underline it.
  1198.      */
  1199.  
  1200.     if ((i == listPtr->active) && (listPtr->flags & GOT_FOCUS)) {
  1201.         Tk_UnderlineChars(listPtr->display, pixmap, gc, listPtr->tkfont,
  1202.             elPtr->text, x, y, 0, elPtr->textLength);
  1203.     }
  1204.     }
  1205.  
  1206.     /*
  1207.      * Redraw the border for the listbox to make sure that it's on top
  1208.      * of any of the text of the listbox entries.
  1209.      */
  1210.  
  1211.     Tk_Draw3DRectangle(tkwin, pixmap, listPtr->normalBorder,
  1212.         listPtr->highlightWidth, listPtr->highlightWidth,
  1213.         Tk_Width(tkwin) - 2*listPtr->highlightWidth,
  1214.         Tk_Height(tkwin) - 2*listPtr->highlightWidth,
  1215.         listPtr->borderWidth, listPtr->relief);
  1216.     if (listPtr->highlightWidth > 0) {
  1217.     GC gc;
  1218.  
  1219.     if (listPtr->flags & GOT_FOCUS) {
  1220.         gc = Tk_GCForColor(listPtr->highlightColorPtr, pixmap);
  1221.     } else {
  1222.         gc = Tk_GCForColor(listPtr->highlightBgColorPtr, pixmap);
  1223.     }
  1224.     Tk_DrawFocusHighlight(tkwin, gc, listPtr->highlightWidth, pixmap);
  1225.     }
  1226.     XCopyArea(listPtr->display, pixmap, Tk_WindowId(tkwin),
  1227.         listPtr->textGC, 0, 0, (unsigned) Tk_Width(tkwin),
  1228.         (unsigned) Tk_Height(tkwin), 0, 0);
  1229.     Tk_FreePixmap(listPtr->display, pixmap);
  1230. }
  1231.  
  1232. /*
  1233.  *----------------------------------------------------------------------
  1234.  *
  1235.  * ListboxComputeGeometry --
  1236.  *
  1237.  *    This procedure is invoked to recompute geometry information
  1238.  *    such as the sizes of the elements and the overall dimensions
  1239.  *    desired for the listbox.
  1240.  *
  1241.  * Results:
  1242.  *    None.
  1243.  *
  1244.  * Side effects:
  1245.  *    Geometry information is updated and a new requested size is
  1246.  *    registered for the widget.  Internal border and gridding
  1247.  *    information is also set.
  1248.  *
  1249.  *----------------------------------------------------------------------
  1250.  */
  1251.  
  1252. static void
  1253. ListboxComputeGeometry(listPtr, fontChanged, maxIsStale, updateGrid)
  1254.     Listbox *listPtr;        /* Listbox whose geometry is to be
  1255.                  * recomputed. */
  1256.     int fontChanged;        /* Non-zero means the font may have changed
  1257.                  * so per-element width information also
  1258.                  * has to be computed. */
  1259.     int maxIsStale;        /* Non-zero means the "maxWidth" field may
  1260.                  * no longer be up-to-date and must
  1261.                  * be recomputed.  If fontChanged is 1 then
  1262.                  * this must be 1. */
  1263.     int updateGrid;        /* Non-zero means call Tk_SetGrid or
  1264.                  * Tk_UnsetGrid to update gridding for
  1265.                  * the window. */
  1266. {
  1267.     register Element *elPtr;
  1268.     int width, height, pixelWidth, pixelHeight;
  1269.     Tk_FontMetrics fm;
  1270.  
  1271.     if (fontChanged  || maxIsStale) {
  1272.     listPtr->xScrollUnit = Tk_TextWidth(listPtr->tkfont, "0", 1);
  1273.     listPtr->maxWidth = 0;
  1274.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  1275.         if (fontChanged) {
  1276.         elPtr->pixelWidth = Tk_TextWidth(listPtr->tkfont,
  1277.             elPtr->text, elPtr->textLength);
  1278.         elPtr->lBearing = 0;
  1279.         }
  1280.         if (elPtr->pixelWidth > listPtr->maxWidth) {
  1281.         listPtr->maxWidth = elPtr->pixelWidth;
  1282.         }
  1283.     }
  1284.     }
  1285.  
  1286.     Tk_GetFontMetrics(listPtr->tkfont, &fm);
  1287.     listPtr->lineHeight = fm.linespace + 1 + 2*listPtr->selBorderWidth;
  1288.     width = listPtr->width;
  1289.     if (width <= 0) {
  1290.     width = (listPtr->maxWidth + listPtr->xScrollUnit - 1)
  1291.         /listPtr->xScrollUnit;
  1292.     if (width < 1) {
  1293.         width = 1;
  1294.     }
  1295.     }
  1296.     pixelWidth = width*listPtr->xScrollUnit + 2*listPtr->inset
  1297.         + 2*listPtr->selBorderWidth;
  1298.     height = listPtr->height;
  1299.     if (listPtr->height <= 0) {
  1300.     height = listPtr->numElements;
  1301.     if (height < 1) {
  1302.         height = 1;
  1303.     }
  1304.     }
  1305.     pixelHeight = height*listPtr->lineHeight + 2*listPtr->inset;
  1306.     Tk_GeometryRequest(listPtr->tkwin, pixelWidth, pixelHeight);
  1307.     Tk_SetInternalBorder(listPtr->tkwin, listPtr->inset);
  1308.     if (updateGrid) {
  1309.     if (listPtr->setGrid) {
  1310.         Tk_SetGrid(listPtr->tkwin, width, height, listPtr->xScrollUnit,
  1311.             listPtr->lineHeight);
  1312.     } else {
  1313.         Tk_UnsetGrid(listPtr->tkwin);
  1314.     }
  1315.     }
  1316. }
  1317.  
  1318. /*
  1319.  *----------------------------------------------------------------------
  1320.  *
  1321.  * InsertEls --
  1322.  *
  1323.  *    Add new elements to a listbox widget.
  1324.  *
  1325.  * Results:
  1326.  *    None.
  1327.  *
  1328.  * Side effects:
  1329.  *    New information gets added to listPtr;  it will be redisplayed
  1330.  *    soon, but not immediately.
  1331.  *
  1332.  *----------------------------------------------------------------------
  1333.  */
  1334.  
  1335. static void
  1336. InsertEls(listPtr, index, argc, argv)
  1337.     register Listbox *listPtr;    /* Listbox that is to get the new
  1338.                  * elements. */
  1339.     int index;            /* Add the new elements before this
  1340.                  * element. */
  1341.     int argc;            /* Number of new elements to add. */
  1342.     char **argv;        /* New elements (one per entry). */
  1343. {
  1344.     register Element *prevPtr, *newPtr;
  1345.     int length, i, oldMaxWidth;
  1346.  
  1347.     /*
  1348.      * Find the element before which the new ones will be inserted.
  1349.      */
  1350.  
  1351.     if (index <= 0) {
  1352.     index = 0;
  1353.     }
  1354.     if (index > listPtr->numElements) {
  1355.     index = listPtr->numElements;
  1356.     }
  1357.     if (index == 0) {
  1358.     prevPtr = NULL;
  1359.     } else if (index == listPtr->numElements) {
  1360.           prevPtr = listPtr->lastPtr;
  1361.     } else {
  1362.     for (prevPtr = listPtr->firstPtr, i = index - 1; i > 0; i--) {
  1363.         prevPtr = prevPtr->nextPtr;
  1364.     }
  1365.     }
  1366.  
  1367.     /*
  1368.      * For each new element, create a record, initialize it, and link
  1369.      * it into the list of elements.
  1370.      */
  1371.  
  1372.     oldMaxWidth = listPtr->maxWidth;
  1373.     for (i = argc ; i > 0; i--, argv++, prevPtr = newPtr) {
  1374.     length = strlen(*argv);
  1375.     newPtr = (Element *) ckalloc(ElementSize(length));
  1376.     newPtr->textLength = length;
  1377.     strcpy(newPtr->text, *argv);
  1378.     newPtr->pixelWidth = Tk_TextWidth(listPtr->tkfont, newPtr->text,
  1379.         newPtr->textLength);
  1380.     newPtr->lBearing = 0;
  1381.     if (newPtr->pixelWidth > listPtr->maxWidth) {
  1382.         listPtr->maxWidth = newPtr->pixelWidth;
  1383.     }
  1384.     newPtr->selected = 0;
  1385.     if (prevPtr == NULL) {
  1386.         newPtr->nextPtr = listPtr->firstPtr;
  1387.         listPtr->firstPtr = newPtr;
  1388.     } else {
  1389.         newPtr->nextPtr = prevPtr->nextPtr;
  1390.         prevPtr->nextPtr = newPtr;
  1391.     }
  1392.     }
  1393.     if ((prevPtr != NULL) && (prevPtr->nextPtr == NULL)) {
  1394.     listPtr->lastPtr = prevPtr;
  1395.     }
  1396.     listPtr->numElements += argc;
  1397.  
  1398.     /*
  1399.      * Update the selection and other indexes to account for the
  1400.      * renumbering that has just occurred.  Then arrange for the new
  1401.      * information to be displayed.
  1402.      */
  1403.  
  1404.     if (index <= listPtr->selectAnchor) {
  1405.     listPtr->selectAnchor += argc;
  1406.     }
  1407.     if (index < listPtr->topIndex) {
  1408.     listPtr->topIndex += argc;
  1409.     }
  1410.     if (index <= listPtr->active) {
  1411.     listPtr->active += argc;
  1412.     if ((listPtr->active >= listPtr->numElements)
  1413.         && (listPtr->numElements > 0)) {
  1414.         listPtr->active = listPtr->numElements-1;
  1415.     }
  1416.     }
  1417.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1418.     if (listPtr->maxWidth != oldMaxWidth) {
  1419.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1420.     }
  1421.     ListboxComputeGeometry(listPtr, 0, 0, 0);
  1422.     ListboxRedrawRange(listPtr, index, listPtr->numElements-1);
  1423. }
  1424.  
  1425. /*
  1426.  *----------------------------------------------------------------------
  1427.  *
  1428.  * DeleteEls --
  1429.  *
  1430.  *    Remove one or more elements from a listbox widget.
  1431.  *
  1432.  * Results:
  1433.  *    None.
  1434.  *
  1435.  * Side effects:
  1436.  *    Memory gets freed, the listbox gets modified and (eventually)
  1437.  *    redisplayed.
  1438.  *
  1439.  *----------------------------------------------------------------------
  1440.  */
  1441.  
  1442. static void
  1443. DeleteEls(listPtr, first, last)
  1444.     register Listbox *listPtr;    /* Listbox widget to modify. */
  1445.     int first;            /* Index of first element to delete. */
  1446.     int last;            /* Index of last element to delete. */
  1447. {
  1448.     register Element *prevPtr, *elPtr;
  1449.     int count, i, widthChanged;
  1450.  
  1451.     /*
  1452.      * Adjust the range to fit within the existing elements of the
  1453.      * listbox, and make sure there's something to delete.
  1454.      */
  1455.  
  1456.     if (first < 0) {
  1457.     first = 0;
  1458.     }
  1459.     if (last >= listPtr->numElements) {
  1460.     last = listPtr->numElements-1;
  1461.     }
  1462.     count = last + 1 - first;
  1463.     if (count <= 0) {
  1464.     return;
  1465.     }
  1466.  
  1467.     /*
  1468.      * Find the element just before the ones to delete.
  1469.      */
  1470.  
  1471.     if (first == 0) {
  1472.     prevPtr = NULL;
  1473.     } else {
  1474.     for (i = first-1, prevPtr = listPtr->firstPtr; i > 0; i--) {
  1475.         prevPtr = prevPtr->nextPtr;
  1476.     }
  1477.     }
  1478.  
  1479.     /*
  1480.      * Delete the requested number of elements.
  1481.      */
  1482.  
  1483.     widthChanged = 0;
  1484.     for (i = count; i > 0; i--) {
  1485.     if (prevPtr == NULL) {
  1486.         elPtr = listPtr->firstPtr;
  1487.         listPtr->firstPtr = elPtr->nextPtr;
  1488.         if (listPtr->firstPtr == NULL) {
  1489.         listPtr->lastPtr = NULL;
  1490.         }
  1491.     } else {
  1492.         elPtr = prevPtr->nextPtr;
  1493.         prevPtr->nextPtr = elPtr->nextPtr;
  1494.         if (prevPtr->nextPtr == NULL) {
  1495.         listPtr->lastPtr = prevPtr;
  1496.         }
  1497.     }
  1498.     if (elPtr->pixelWidth == listPtr->maxWidth) {
  1499.         widthChanged = 1;
  1500.     }
  1501.     if (elPtr->selected) {
  1502.         listPtr->numSelected -= 1;
  1503.     }
  1504.     ckfree((char *) elPtr);
  1505.     }
  1506.     listPtr->numElements -= count;
  1507.  
  1508.     /*
  1509.      * Update the selection and viewing information to reflect the change
  1510.      * in the element numbering, and redisplay to slide information up over
  1511.      * the elements that were deleted.
  1512.      */
  1513.  
  1514.     if (first <= listPtr->selectAnchor) {
  1515.     listPtr->selectAnchor -= count;
  1516.     if (listPtr->selectAnchor < first) {
  1517.         listPtr->selectAnchor = first;
  1518.     }
  1519.     }
  1520.     if (first <= listPtr->topIndex) {
  1521.     listPtr->topIndex -= count;
  1522.     if (listPtr->topIndex < first) {
  1523.         listPtr->topIndex = first;
  1524.     }
  1525.     }
  1526.     if (listPtr->topIndex > (listPtr->numElements - listPtr->fullLines)) {
  1527.     listPtr->topIndex = listPtr->numElements - listPtr->fullLines;
  1528.     if (listPtr->topIndex < 0) {
  1529.         listPtr->topIndex = 0;
  1530.     }
  1531.     }
  1532.     if (listPtr->active > last) {
  1533.     listPtr->active -= count;
  1534.     } else if (listPtr->active >= first) {
  1535.     listPtr->active = first;
  1536.     if ((listPtr->active >= listPtr->numElements)
  1537.         && (listPtr->numElements > 0)) {
  1538.         listPtr->active = listPtr->numElements-1;
  1539.     }
  1540.     }
  1541.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1542.     ListboxComputeGeometry(listPtr, 0, widthChanged, 0);
  1543.     if (widthChanged) {
  1544.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1545.     }
  1546.     ListboxRedrawRange(listPtr, first, listPtr->numElements-1);
  1547. }
  1548.  
  1549. /*
  1550.  *--------------------------------------------------------------
  1551.  *
  1552.  * ListboxEventProc --
  1553.  *
  1554.  *    This procedure is invoked by the Tk dispatcher for various
  1555.  *    events on listboxes.
  1556.  *
  1557.  * Results:
  1558.  *    None.
  1559.  *
  1560.  * Side effects:
  1561.  *    When the window gets deleted, internal structures get
  1562.  *    cleaned up.  When it gets exposed, it is redisplayed.
  1563.  *
  1564.  *--------------------------------------------------------------
  1565.  */
  1566.  
  1567. static void
  1568. ListboxEventProc(clientData, eventPtr)
  1569.     ClientData clientData;    /* Information about window. */
  1570.     XEvent *eventPtr;        /* Information about event. */
  1571. {
  1572.     Listbox *listPtr = (Listbox *) clientData;
  1573.  
  1574.     if (eventPtr->type == Expose) {
  1575.     ListboxRedrawRange(listPtr,
  1576.         NearestListboxElement(listPtr, eventPtr->xexpose.y),
  1577.         NearestListboxElement(listPtr, eventPtr->xexpose.y
  1578.         + eventPtr->xexpose.height));
  1579.     } else if (eventPtr->type == DestroyNotify) {
  1580.     if (listPtr->tkwin != NULL) {
  1581.         if (listPtr->setGrid) {
  1582.         Tk_UnsetGrid(listPtr->tkwin);
  1583.         }
  1584.         listPtr->tkwin = NULL;
  1585.         Tcl_DeleteCommandFromToken(listPtr->interp, listPtr->widgetCmd);
  1586.     }
  1587.     if (listPtr->flags & REDRAW_PENDING) {
  1588.         Tcl_CancelIdleCall(DisplayListbox, (ClientData) listPtr);
  1589.     }
  1590.     Tcl_EventuallyFree((ClientData) listPtr, DestroyListbox);
  1591.     } else if (eventPtr->type == ConfigureNotify) {
  1592.     int vertSpace;
  1593.  
  1594.     vertSpace = Tk_Height(listPtr->tkwin) - 2*listPtr->inset;
  1595.     listPtr->fullLines = vertSpace / listPtr->lineHeight;
  1596.     if ((listPtr->fullLines*listPtr->lineHeight) < vertSpace) {
  1597.         listPtr->partialLine = 1;
  1598.     } else {
  1599.         listPtr->partialLine = 0;
  1600.     }
  1601.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1602.     ChangeListboxView(listPtr, listPtr->topIndex);
  1603.     ChangeListboxOffset(listPtr, listPtr->xOffset);
  1604.  
  1605.     /*
  1606.      * Redraw the whole listbox.  It's hard to tell what needs
  1607.      * to be redrawn (e.g. if the listbox has shrunk then we
  1608.      * may only need to redraw the borders), so just redraw
  1609.      * everything for safety.
  1610.      */
  1611.  
  1612.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1613.     } else if (eventPtr->type == FocusIn) {
  1614.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1615.         listPtr->flags |= GOT_FOCUS;
  1616.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1617.     }
  1618.     } else if (eventPtr->type == FocusOut) {
  1619.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1620.         listPtr->flags &= ~GOT_FOCUS;
  1621.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1622.     }
  1623.     }
  1624. }
  1625.  
  1626. /*
  1627.  *----------------------------------------------------------------------
  1628.  *
  1629.  * ListboxCmdDeletedProc --
  1630.  *
  1631.  *    This procedure is invoked when a widget command is deleted.  If
  1632.  *    the widget isn't already in the process of being destroyed,
  1633.  *    this command destroys it.
  1634.  *
  1635.  * Results:
  1636.  *    None.
  1637.  *
  1638.  * Side effects:
  1639.  *    The widget is destroyed.
  1640.  *
  1641.  *----------------------------------------------------------------------
  1642.  */
  1643.  
  1644. static void
  1645. ListboxCmdDeletedProc(clientData)
  1646.     ClientData clientData;    /* Pointer to widget record for widget. */
  1647. {
  1648.     Listbox *listPtr = (Listbox *) clientData;
  1649.     Tk_Window tkwin = listPtr->tkwin;
  1650.  
  1651.     /*
  1652.      * This procedure could be invoked either because the window was
  1653.      * destroyed and the command was then deleted (in which case tkwin
  1654.      * is NULL) or because the command was deleted, and then this procedure
  1655.      * destroys the widget.
  1656.      */
  1657.  
  1658.     if (tkwin != NULL) {
  1659.     if (listPtr->setGrid) {
  1660.         Tk_UnsetGrid(listPtr->tkwin);
  1661.     }
  1662.     listPtr->tkwin = NULL;
  1663.     Tk_DestroyWindow(tkwin);
  1664.     }
  1665. }
  1666.  
  1667. /*
  1668.  *--------------------------------------------------------------
  1669.  *
  1670.  * GetListboxIndex --
  1671.  *
  1672.  *    Parse an index into a listbox and return either its value
  1673.  *    or an error.
  1674.  *
  1675.  * Results:
  1676.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1677.  *    filled in with the index (into listPtr) corresponding to
  1678.  *    string.  Otherwise an error message is left in interp->result.
  1679.  *
  1680.  * Side effects:
  1681.  *    None.
  1682.  *
  1683.  *--------------------------------------------------------------
  1684.  */
  1685.  
  1686. static int
  1687. GetListboxIndex(interp, listPtr, string, numElsOK, indexPtr)
  1688.     Tcl_Interp *interp;        /* For error messages. */
  1689.     Listbox *listPtr;        /* Listbox for which the index is being
  1690.                  * specified. */
  1691.     char *string;        /* Specifies an element in the listbox. */
  1692.     int numElsOK;        /* 0 means the return value must be less
  1693.                  * less than the number of entries in
  1694.                  * the listbox;  1 means it may also be
  1695.                  * equal to the number of entries. */
  1696.     int *indexPtr;        /* Where to store converted index. */
  1697. {
  1698.     int c;
  1699.     size_t length;
  1700.  
  1701.     length = strlen(string);
  1702.     c = string[0];
  1703.     if ((c == 'a') && (strncmp(string, "active", length) == 0)
  1704.         && (length >= 2)) {
  1705.     *indexPtr = listPtr->active;
  1706.     } else if ((c == 'a') && (strncmp(string, "anchor", length) == 0)
  1707.         && (length >= 2)) {
  1708.     *indexPtr = listPtr->selectAnchor;
  1709.     } else if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
  1710.     *indexPtr = listPtr->numElements;
  1711.     } else if (c == '@') {
  1712.     int y;
  1713.     char *p, *end;
  1714.  
  1715.     p = string+1;
  1716.     strtol(p, &end, 0);
  1717.     if ((end == p) || (*end != ',')) {
  1718.         goto badIndex;
  1719.     }
  1720.     p = end+1;
  1721.     y = strtol(p, &end, 0);
  1722.     if ((end == p) || (*end != 0)) {
  1723.         goto badIndex;
  1724.     }
  1725.     *indexPtr = NearestListboxElement(listPtr, y);
  1726.     } else {
  1727.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1728.         Tcl_ResetResult(interp);
  1729.         goto badIndex;
  1730.     }
  1731.     }
  1732.     if (numElsOK) {
  1733.     if (*indexPtr > listPtr->numElements) {
  1734.         *indexPtr = listPtr->numElements;
  1735.     }
  1736.     } else if (*indexPtr >= listPtr->numElements) {
  1737.     *indexPtr = listPtr->numElements-1;
  1738.     }
  1739.     if (*indexPtr < 0) {
  1740.     *indexPtr = 0;
  1741.     }
  1742.     return TCL_OK;
  1743.  
  1744.     badIndex:
  1745.     Tcl_AppendResult(interp, "bad listbox index \"", string,
  1746.         "\": must be active, anchor, end, @x,y, or a number",
  1747.         (char *) NULL);
  1748.     return TCL_ERROR;
  1749. }
  1750.  
  1751. /*
  1752.  *----------------------------------------------------------------------
  1753.  *
  1754.  * ChangeListboxView --
  1755.  *
  1756.  *    Change the view on a listbox widget so that a given element
  1757.  *    is displayed at the top.
  1758.  *
  1759.  * Results:
  1760.  *    None.
  1761.  *
  1762.  * Side effects:
  1763.  *    What's displayed on the screen is changed.  If there is a
  1764.  *    scrollbar associated with this widget, then the scrollbar
  1765.  *    is instructed to change its display too.
  1766.  *
  1767.  *----------------------------------------------------------------------
  1768.  */
  1769.  
  1770. static void
  1771. ChangeListboxView(listPtr, index)
  1772.     register Listbox *listPtr;        /* Information about widget. */
  1773.     int index;                /* Index of element in listPtr
  1774.                      * that should now appear at the
  1775.                      * top of the listbox. */
  1776. {
  1777.     if (index >= (listPtr->numElements - listPtr->fullLines)) {
  1778.     index = listPtr->numElements - listPtr->fullLines;
  1779.     }
  1780.     if (index < 0) {
  1781.     index = 0;
  1782.     }
  1783.     if (listPtr->topIndex != index) {
  1784.     listPtr->topIndex = index;
  1785.     if (!(listPtr->flags & REDRAW_PENDING)) {
  1786.         Tcl_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  1787.         listPtr->flags |= REDRAW_PENDING;
  1788.     }
  1789.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1790.     }
  1791. }
  1792.  
  1793. /*
  1794.  *----------------------------------------------------------------------
  1795.  *
  1796.  * ChangListboxOffset --
  1797.  *
  1798.  *    Change the horizontal offset for a listbox.
  1799.  *
  1800.  * Results:
  1801.  *    None.
  1802.  *
  1803.  * Side effects:
  1804.  *    The listbox may be redrawn to reflect its new horizontal
  1805.  *    offset.
  1806.  *
  1807.  *----------------------------------------------------------------------
  1808.  */
  1809.  
  1810. static void
  1811. ChangeListboxOffset(listPtr, offset)
  1812.     register Listbox *listPtr;        /* Information about widget. */
  1813.     int offset;                /* Desired new "xOffset" for
  1814.                      * listbox. */
  1815. {
  1816.     int maxOffset;
  1817.  
  1818.     /*
  1819.      * Make sure that the new offset is within the allowable range, and
  1820.      * round it off to an even multiple of xScrollUnit.
  1821.      */
  1822.  
  1823.     maxOffset = listPtr->maxWidth - (Tk_Width(listPtr->tkwin) -
  1824.         2*listPtr->inset - 2*listPtr->selBorderWidth) - 1;
  1825.     if (offset > maxOffset) {
  1826.     offset = maxOffset;
  1827.     }
  1828.     if (offset < 0) {
  1829.     offset = 0;
  1830.     }
  1831.     offset -= offset%listPtr->xScrollUnit;
  1832.     if (offset != listPtr->xOffset) {
  1833.     listPtr->xOffset = offset;
  1834.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1835.     ListboxRedrawRange(listPtr, 0, listPtr->numElements);
  1836.     }
  1837. }
  1838.  
  1839. /*
  1840.  *----------------------------------------------------------------------
  1841.  *
  1842.  * ListboxScanTo --
  1843.  *
  1844.  *    Given a point (presumably of the curent mouse location)
  1845.  *    drag the view in the window to implement the scan operation.
  1846.  *
  1847.  * Results:
  1848.  *    None.
  1849.  *
  1850.  * Side effects:
  1851.  *    The view in the window may change.
  1852.  *
  1853.  *----------------------------------------------------------------------
  1854.  */
  1855.  
  1856. static void
  1857. ListboxScanTo(listPtr, x, y)
  1858.     register Listbox *listPtr;        /* Information about widget. */
  1859.     int x;                /* X-coordinate to use for scan
  1860.                      * operation. */
  1861.     int y;                /* Y-coordinate to use for scan
  1862.                      * operation. */
  1863. {
  1864.     int newTopIndex, newOffset, maxIndex, maxOffset;
  1865.  
  1866.     maxIndex = listPtr->numElements - listPtr->fullLines;
  1867.     maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit-1)
  1868.         - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
  1869.         - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
  1870.  
  1871.     /*
  1872.      * Compute new top line for screen by amplifying the difference
  1873.      * between the current position and the place where the scan
  1874.      * started (the "mark" position).  If we run off the top or bottom
  1875.      * of the list, then reset the mark point so that the current
  1876.      * position continues to correspond to the edge of the window.
  1877.      * This means that the picture will start dragging as soon as the
  1878.      * mouse reverses direction (without this reset, might have to slide
  1879.      * mouse a long ways back before the picture starts moving again).
  1880.      */
  1881.  
  1882.     newTopIndex = listPtr->scanMarkYIndex
  1883.         - (10*(y - listPtr->scanMarkY))/listPtr->lineHeight;
  1884.     if (newTopIndex > maxIndex) {
  1885.     newTopIndex = listPtr->scanMarkYIndex = maxIndex;
  1886.     listPtr->scanMarkY = y;
  1887.     } else if (newTopIndex < 0) {
  1888.     newTopIndex = listPtr->scanMarkYIndex = 0;
  1889.     listPtr->scanMarkY = y;
  1890.     }
  1891.     ChangeListboxView(listPtr, newTopIndex);
  1892.  
  1893.     /*
  1894.      * Compute new left edge for display in a similar fashion by amplifying
  1895.      * the difference between the current position and the place where the
  1896.      * scan started.
  1897.      */
  1898.  
  1899.     newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
  1900.     if (newOffset > maxOffset) {
  1901.     newOffset = listPtr->scanMarkXOffset = maxOffset;
  1902.     listPtr->scanMarkX = x;
  1903.     } else if (newOffset < 0) {
  1904.     newOffset = listPtr->scanMarkXOffset = 0;
  1905.     listPtr->scanMarkX = x;
  1906.     }
  1907.     ChangeListboxOffset(listPtr, newOffset);
  1908. }
  1909.  
  1910. /*
  1911.  *----------------------------------------------------------------------
  1912.  *
  1913.  * NearestListboxElement --
  1914.  *
  1915.  *    Given a y-coordinate inside a listbox, compute the index of
  1916.  *    the element under that y-coordinate (or closest to that
  1917.  *    y-coordinate).
  1918.  *
  1919.  * Results:
  1920.  *    The return value is an index of an element of listPtr.  If
  1921.  *    listPtr has no elements, then 0 is always returned.
  1922.  *
  1923.  * Side effects:
  1924.  *    None.
  1925.  *
  1926.  *----------------------------------------------------------------------
  1927.  */
  1928.  
  1929. static int
  1930. NearestListboxElement(listPtr, y)
  1931.     register Listbox *listPtr;        /* Information about widget. */
  1932.     int y;                /* Y-coordinate in listPtr's window. */
  1933. {
  1934.     int index;
  1935.  
  1936.     index = (y - listPtr->inset)/listPtr->lineHeight;
  1937.     if (index >= (listPtr->fullLines + listPtr->partialLine)) {
  1938.     index = listPtr->fullLines + listPtr->partialLine - 1;
  1939.     }
  1940.     if (index < 0) {
  1941.     index = 0;
  1942.     }
  1943.     index += listPtr->topIndex;
  1944.     if (index >= listPtr->numElements) {
  1945.     index = listPtr->numElements-1;
  1946.     }
  1947.     return index;
  1948. }
  1949.  
  1950. /*
  1951.  *----------------------------------------------------------------------
  1952.  *
  1953.  * ListboxSelect --
  1954.  *
  1955.  *    Select or deselect one or more elements in a listbox..
  1956.  *
  1957.  * Results:
  1958.  *    None.
  1959.  *
  1960.  * Side effects:
  1961.  *    All of the elements in the range between first and last are
  1962.  *    marked as either selected or deselected, depending on the
  1963.  *    "select" argument.  Any items whose state changes are redisplayed.
  1964.  *    The selection is claimed from X when the number of selected
  1965.  *    elements changes from zero to non-zero.
  1966.  *
  1967.  *----------------------------------------------------------------------
  1968.  */
  1969.  
  1970. static void
  1971. ListboxSelect(listPtr, first, last, select)
  1972.     register Listbox *listPtr;        /* Information about widget. */
  1973.     int first;                /* Index of first element to
  1974.                      * select or deselect. */
  1975.     int last;                /* Index of last element to
  1976.                      * select or deselect. */
  1977.     int select;                /* 1 means select items, 0 means
  1978.                      * deselect them. */
  1979. {
  1980.     int i, firstRedisplay, increment, oldCount;
  1981.     Element *elPtr;
  1982.  
  1983.     if (last < first) {
  1984.     i = first;
  1985.     first = last;
  1986.     last = i;
  1987.     }
  1988.     if (first >= listPtr->numElements) {
  1989.     return;
  1990.     }
  1991.     oldCount = listPtr->numSelected;
  1992.     firstRedisplay = -1;
  1993.     increment = select ? 1 : -1;
  1994.     for (i = 0, elPtr = listPtr->firstPtr; i < first;
  1995.         i++, elPtr = elPtr->nextPtr) {
  1996.     /* Empty loop body. */
  1997.     }
  1998.     for ( ; i <= last; i++, elPtr = elPtr->nextPtr) {
  1999.     if (elPtr->selected == select) {
  2000.         continue;
  2001.     }
  2002.     listPtr->numSelected += increment;
  2003.     elPtr->selected = select;
  2004.     if (firstRedisplay < 0) {
  2005.         firstRedisplay = i;
  2006.     }
  2007.     }
  2008.     if (firstRedisplay >= 0) {
  2009.     ListboxRedrawRange(listPtr, first, last);
  2010.     }
  2011.     if ((oldCount == 0) && (listPtr->numSelected > 0)
  2012.         && (listPtr->exportSelection)) {
  2013.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  2014.         (ClientData) listPtr);
  2015.     }
  2016. }
  2017.  
  2018. /*
  2019.  *----------------------------------------------------------------------
  2020.  *
  2021.  * ListboxFetchSelection --
  2022.  *
  2023.  *    This procedure is called back by Tk when the selection is
  2024.  *    requested by someone.  It returns part or all of the selection
  2025.  *    in a buffer provided by the caller.
  2026.  *
  2027.  * Results:
  2028.  *    The return value is the number of non-NULL bytes stored
  2029.  *    at buffer.  Buffer is filled (or partially filled) with a
  2030.  *    NULL-terminated string containing part or all of the selection,
  2031.  *    as given by offset and maxBytes.  The selection is returned
  2032.  *    as a Tcl list with one list element for each element in the
  2033.  *    listbox.
  2034.  *
  2035.  * Side effects:
  2036.  *    None.
  2037.  *
  2038.  *----------------------------------------------------------------------
  2039.  */
  2040.  
  2041. static int
  2042. ListboxFetchSelection(clientData, offset, buffer, maxBytes)
  2043.     ClientData clientData;        /* Information about listbox widget. */
  2044.     int offset;                /* Offset within selection of first
  2045.                      * byte to be returned. */
  2046.     char *buffer;            /* Location in which to place
  2047.                      * selection. */
  2048.     int maxBytes;            /* Maximum number of bytes to place
  2049.                      * at buffer, not including terminating
  2050.                      * NULL character. */
  2051. {
  2052.     register Listbox *listPtr = (Listbox *) clientData;
  2053.     register Element *elPtr;
  2054.     Tcl_DString selection;
  2055.     int length, count, needNewline;
  2056.  
  2057.     if (!listPtr->exportSelection) {
  2058.     return -1;
  2059.     }
  2060.  
  2061.     /*
  2062.      * Use a dynamic string to accumulate the contents of the selection.
  2063.      */
  2064.  
  2065.     needNewline = 0;
  2066.     Tcl_DStringInit(&selection);
  2067.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  2068.     if (elPtr->selected) {
  2069.         if (needNewline) {
  2070.         Tcl_DStringAppend(&selection, "\n", 1);
  2071.         }
  2072.         Tcl_DStringAppend(&selection, elPtr->text, elPtr->textLength);
  2073.         needNewline = 1;
  2074.     }
  2075.     }
  2076.  
  2077.     length = Tcl_DStringLength(&selection);
  2078.     if (length == 0) {
  2079.     return -1;
  2080.     }
  2081.  
  2082.     /*
  2083.      * Copy the requested portion of the selection to the buffer.
  2084.      */
  2085.  
  2086.     count = length - offset;
  2087.     if (count <= 0) {
  2088.     count = 0;
  2089.     } else {
  2090.     if (count > maxBytes) {
  2091.         count = maxBytes;
  2092.     }
  2093.     memcpy((VOID *) buffer,
  2094.         (VOID *) (Tcl_DStringValue(&selection) + offset),
  2095.         (size_t) count);
  2096.     }
  2097.     buffer[count] = '\0';
  2098.     Tcl_DStringFree(&selection);
  2099.     return count;
  2100. }
  2101.  
  2102. /*
  2103.  *----------------------------------------------------------------------
  2104.  *
  2105.  * ListboxLostSelection --
  2106.  *
  2107.  *    This procedure is called back by Tk when the selection is
  2108.  *    grabbed away from a listbox widget.
  2109.  *
  2110.  * Results:
  2111.  *    None.
  2112.  *
  2113.  * Side effects:
  2114.  *    The existing selection is unhighlighted, and the window is
  2115.  *    marked as not containing a selection.
  2116.  *
  2117.  *----------------------------------------------------------------------
  2118.  */
  2119.  
  2120. static void
  2121. ListboxLostSelection(clientData)
  2122.     ClientData clientData;        /* Information about listbox widget. */
  2123. {
  2124.     register Listbox *listPtr = (Listbox *) clientData;
  2125.  
  2126.     if ((listPtr->exportSelection) && (listPtr->numElements > 0)) {
  2127.     ListboxSelect(listPtr, 0, listPtr->numElements-1, 0);
  2128.     }
  2129. }
  2130.  
  2131. /*
  2132.  *----------------------------------------------------------------------
  2133.  *
  2134.  * ListboxRedrawRange --
  2135.  *
  2136.  *    Ensure that a given range of elements is eventually redrawn on
  2137.  *    the display (if those elements in fact appear on the display).
  2138.  *
  2139.  * Results:
  2140.  *    None.
  2141.  *
  2142.  * Side effects:
  2143.  *    Information gets redisplayed.
  2144.  *
  2145.  *----------------------------------------------------------------------
  2146.  */
  2147.  
  2148.     /* ARGSUSED */
  2149. static void
  2150. ListboxRedrawRange(listPtr, first, last)
  2151.     register Listbox *listPtr;        /* Information about widget. */
  2152.     int first;                /* Index of first element in list
  2153.                      * that needs to be redrawn. */
  2154.     int last;                /* Index of last element in list
  2155.                      * that needs to be redrawn.  May
  2156.                      * be less than first;
  2157.                      * these just bracket a range. */
  2158. {
  2159.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(listPtr->tkwin)
  2160.         || (listPtr->flags & REDRAW_PENDING)) {
  2161.     return;
  2162.     }
  2163.     Tcl_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  2164.     listPtr->flags |= REDRAW_PENDING;
  2165. }
  2166.  
  2167. /*
  2168.  *----------------------------------------------------------------------
  2169.  *
  2170.  * ListboxUpdateVScrollbar --
  2171.  *
  2172.  *    This procedure is invoked whenever information has changed in
  2173.  *    a listbox in a way that would invalidate a vertical scrollbar
  2174.  *    display.  If there is an associated scrollbar, then this command
  2175.  *    updates it by invoking a Tcl command.
  2176.  *
  2177.  * Results:
  2178.  *    None.
  2179.  *
  2180.  * Side effects:
  2181.  *    A Tcl command is invoked, and an additional command may be
  2182.  *    invoked to process errors in the command.
  2183.  *
  2184.  *----------------------------------------------------------------------
  2185.  */
  2186.  
  2187. static void
  2188. ListboxUpdateVScrollbar(listPtr)
  2189.     register Listbox *listPtr;        /* Information about widget. */
  2190. {
  2191.     char string[100];
  2192.     double first, last;
  2193.     int result;
  2194.     Tcl_Interp *interp;
  2195.  
  2196.     if (listPtr->yScrollCmd == NULL) {
  2197.     return;
  2198.     }
  2199.     if (listPtr->numElements == 0) {
  2200.     first = 0.0;
  2201.     last = 1.0;
  2202.     } else {
  2203.     first = listPtr->topIndex/((double) listPtr->numElements);
  2204.     last = (listPtr->topIndex+listPtr->fullLines)
  2205.         /((double) listPtr->numElements);
  2206.     if (last > 1.0) {
  2207.         last = 1.0;
  2208.     }
  2209.     }
  2210.     sprintf(string, " %g %g", first, last);
  2211.  
  2212.     /*
  2213.      * We must hold onto the interpreter from the listPtr because the data
  2214.      * at listPtr might be freed as a result of the Tcl_VarEval.
  2215.      */
  2216.     
  2217.     interp = listPtr->interp;
  2218.     Tcl_Preserve((ClientData) interp);
  2219.     result = Tcl_VarEval(interp, listPtr->yScrollCmd, string,
  2220.         (char *) NULL);
  2221.     if (result != TCL_OK) {
  2222.     Tcl_AddErrorInfo(interp,
  2223.         "\n    (vertical scrolling command executed by listbox)");
  2224.     Tcl_BackgroundError(interp);
  2225.     }
  2226.     Tcl_Release((ClientData) interp);
  2227. }
  2228.  
  2229. /*
  2230.  *----------------------------------------------------------------------
  2231.  *
  2232.  * ListboxUpdateHScrollbar --
  2233.  *
  2234.  *    This procedure is invoked whenever information has changed in
  2235.  *    a listbox in a way that would invalidate a horizontal scrollbar
  2236.  *    display.  If there is an associated horizontal scrollbar, then
  2237.  *    this command updates it by invoking a Tcl command.
  2238.  *
  2239.  * Results:
  2240.  *    None.
  2241.  *
  2242.  * Side effects:
  2243.  *    A Tcl command is invoked, and an additional command may be
  2244.  *    invoked to process errors in the command.
  2245.  *
  2246.  *----------------------------------------------------------------------
  2247.  */
  2248.  
  2249. static void
  2250. ListboxUpdateHScrollbar(listPtr)
  2251.     register Listbox *listPtr;        /* Information about widget. */
  2252. {
  2253.     char string[60];
  2254.     int result, windowWidth;
  2255.     double first, last;
  2256.     Tcl_Interp *interp;
  2257.  
  2258.     if (listPtr->xScrollCmd == NULL) {
  2259.     return;
  2260.     }
  2261.     windowWidth = Tk_Width(listPtr->tkwin) - 2*(listPtr->inset
  2262.         + listPtr->selBorderWidth);
  2263.     if (listPtr->maxWidth == 0) {
  2264.     first = 0;
  2265.     last = 1.0;
  2266.     } else {
  2267.     first = listPtr->xOffset/((double) listPtr->maxWidth);
  2268.     last = (listPtr->xOffset + windowWidth)
  2269.         /((double) listPtr->maxWidth);
  2270.     if (last > 1.0) {
  2271.         last = 1.0;
  2272.     }
  2273.     }
  2274.     sprintf(string, " %g %g", first, last);
  2275.  
  2276.     /*
  2277.      * We must hold onto the interpreter because the data referred to at
  2278.      * listPtr might be freed as a result of the call to Tcl_VarEval.
  2279.      */
  2280.     
  2281.     interp = listPtr->interp;
  2282.     Tcl_Preserve((ClientData) interp);
  2283.     result = Tcl_VarEval(interp, listPtr->xScrollCmd, string,
  2284.         (char *) NULL);
  2285.     if (result != TCL_OK) {
  2286.     Tcl_AddErrorInfo(interp,
  2287.         "\n    (horizontal scrolling command executed by listbox)");
  2288.     Tcl_BackgroundError(interp);
  2289.     }
  2290.     Tcl_Release((ClientData) interp);
  2291. }
  2292.